package com.zhangyongyi.carhome.adapter.recommend.newes;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageCache;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.Volley;
import com.zhangyongyi.carhome.R;
import com.zhangyongyi.carhome.entity.greendao.NewesListEntity;
import com.zhangyongyi.carhome.tools.MyOnClickListener;

import java.util.List;

/**
 * Created by dllo on 16/1/11.
 * 最新的适配器RecyclerView
 */
public class NewestRecyclerViewAdapter extends RecyclerView.Adapter<NewestRecyclerViewAdapter.MyViewHolder> {
    private List<NewesListEntity> newsListEntities;
    private RequestQueue queue;
    private LruCache<String, Bitmap> lruCache;    //*
    private ImageCache imageCache;    //*
    private ImageLoader imageLoader;  //*

    //*----*
    private MyOnClickListener myOnClickListener;

    public void setMyOnClickListener(MyOnClickListener myOnClickListener) {
        this.myOnClickListener = myOnClickListener;
    }

    public NewestRecyclerViewAdapter(Context context) {
        queue = Volley.newRequestQueue(context);
        lruCache = new LruCache<String, Bitmap>(20);
        imageCache = new ImageCache() {

            public void putBitmap(String url, Bitmap bitmap) {
                lruCache.put(url, bitmap);
            }


            @Override
            public Bitmap getBitmap(String url) {
                return lruCache.get(url);
            }
        };
        imageLoader = new ImageLoader(queue, imageCache);
    }

    public void addData(List<NewesListEntity> datas) {
        newsListEntities = datas;
        notifyDataSetChanged();
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int po) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View view = inflater.inflate(R.layout.item_recommend_newest,null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (newsListEntities != null && newsListEntities.size() > 0){
            final NewesListEntity newesListEntity = newsListEntities.get(position);
            holder.title.setText(newesListEntity.getTitle());
            holder.time.setText(newesListEntity.getTime());
            holder.replyCount.setText(String.valueOf(newesListEntity.getReplycount()));
            if (newesListEntity.getSmallpic() != null && newesListEntity.getSmallpic().length() > 0){
                String imgUrl = newesListEntity.getSmallpic();
                //网络拉取图片并显示

                getBitmapCache(imgUrl, holder.imgIv);
            }
            //*----*
            holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myOnClickListener.MyOnClickListener(newesListEntity.getId());
            }
        });
    }

    }
    public void getBitmapCache(String url, ImageView imageView) {
        //* 构建图片监听器
        ImageListener listener = ImageLoader.getImageListener(
                imageView, R.mipmap.nissan,
                R.mipmap.nissan);
        //* 图片拉取
        imageLoader.get(url, listener);	//*
    }


    @Override
    public int getItemCount() {
        return newsListEntities != null && newsListEntities.size() > 0 ? newsListEntities.size() : 0;
    }



    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView imgIv;
        private TextView title,time,replyCount;


        public MyViewHolder(View itemView) {

            super(itemView);
            imgIv = (ImageView) itemView.findViewById(R.id.iv_newest_item);
            title = (TextView) itemView.findViewById(R.id.tv_title_item_newest);
            time = (TextView) itemView.findViewById(R.id.tv_time_item_newest);
            replyCount = (TextView) itemView.findViewById(R.id.tv_replyCount_item_newest);


        }
    }
}
